const httpUtils = require('../../../utils/httpUtil')

Page({
  data: {
    deviceList: [],
    callType: ''
  },

  onLoad(options) {
    this.setData({
      callType: options.callType
    })
    console.log('呼叫类型：'+options.callType);
  },

  onShow() {
    this.queryMyGroup();
    this.queryMyGroup2();
  },

  // 多选框点击事件
  onCheckboxTap(e) {
    const index = e.currentTarget.dataset.index; // 获取点击的索引
    const list = this.data.deviceList;
    list[index].checked = !list[index].checked; // 切换选中状态
    this.setData({ list });
  },

  // 开始按钮点击事件
  onStartTap() {
    const selectedItems = this.data.deviceList.filter((item) => item.checked); // 获取选中的项
    if (selectedItems.length === 0) {
      wx.showToast({
        title: "请至少选择一个分组",
        icon: "none",
      });
    } else {
      const numbers = selectedItems.map(item => item.deviceUid);
      console.log("呼叫:"+numbers);
      if (this.data.callType == 'voice') {
        wx.navigateTo({
          url: '/pages/call/voice-call/voice-call?numbers='+encodeURIComponent(JSON.stringify(numbers)),
        });
      } else {
        wx.navigateTo({
          url: '/pages/call/text-call/text-call?numbers='+encodeURIComponent(JSON.stringify(numbers)),
        });
      }
      console.log('跳转结束');
    }
  },

  onCancelTap() {
    wx.navigateBack();
  },

  queryMyGroup() {
    httpUtils.request({
      'method': 'GET',
      'url': '/prod-api/interface/getDevicesByDeptId',
      'showLoading': true,
      'message': '获取分组设备...'
    }).then(res=>{
      if (res.data.code = 200) {
        this.setData({
          deviceList: res.data.data
        });
      }
    });
  },

  queryMyGroup2() {
    httpUtils.request({
      'method': 'GET',
      'url': '/prod-api/interface/getUserDept',
      'showLoading': true,
      'message': '获取分组设备...'
    }).then(res=>{
      if (res.data.code = 200) {
        
      }
    });
  }
});